package cn.xiongxf.system.controller;

import cn.xiongxf.basic.util.AjaxResult;
import cn.xiongxf.basic.util.PageList;
import cn.xiongxf.system.domain.SystemDictionaryDetail;
import cn.xiongxf.system.query.SystemDictionaryDetailQuery;
import cn.xiongxf.system.service.SystemDictionaryDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/systemDictionaryDetail")
@Api(value = "数据字典明细表API",description = "数据字典明细表的CRUD操作") // 描述Controller
public class SystemDictionaryDetailController {
    @Autowired
    private SystemDictionaryDetailService systemDictionaryDetailService;

    /**
     * 接口：查询所有
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有") // 描述方法
    public List<SystemDictionaryDetail> findAll(){
        return systemDictionaryDetailService.findAll();
    }

    /**
     * 接口：查询单个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个")
    public SystemDictionaryDetail findById(@PathVariable Long id){
        return systemDictionaryDetailService.findById(id);
    }

    /**
     * 接口：根据types_id查询
     * @param types_id
     * @return
     */
    @GetMapping("/type/{types_id}")
    @ApiOperation(value = "根据types_id查询")
    public List<SystemDictionaryDetail> findByTypesId(@PathVariable Long types_id){
        return systemDictionaryDetailService.findByTypesId(types_id);
    }

    /**
     * 接口：分页查询
     * @param systemDictionaryDetailQuery
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询")
    public PageList<SystemDictionaryDetail> queryPage(@RequestBody SystemDictionaryDetailQuery systemDictionaryDetailQuery){
        return systemDictionaryDetailService.queryPageList(systemDictionaryDetailQuery);
    }

    /**
     * 接口：删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public AjaxResult del(@PathVariable Long id){
        try {
            systemDictionaryDetailService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 接口：批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try{
            systemDictionaryDetailService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 接口：新增/修改
     * @param systemDictionaryDetail
     * @return
     */
    @PutMapping
    @ApiOperation(value = "新增/修改")
    public AjaxResult addOrUpdate(@RequestBody SystemDictionaryDetail systemDictionaryDetail){
        try {
            if (systemDictionaryDetail.getId() == null){
                systemDictionaryDetailService.add(systemDictionaryDetail);
            }else {
                systemDictionaryDetailService.update(systemDictionaryDetail);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

}
